<?php

namespace App\Repositories\Api;

use App\Models\MemberMiniRecommend;
use App\Models\MemberRecommend;
use App\Models\Member;

use App\Models\MemberRecommendException;
use Exception;

class MemberMiniRecommendRepository
{
    public function getPages($filter = [], int $pageSize = 10, int $pageNum = 1, $is_sort = "")
    {
        $res = MemberRecommend::query()
            ->join("member", "member.id", "=", "member_recommend.uid")
            ->where("member_recommend.ruid", "=", $filter['ruid'])
            ->orderBy("create_time", "desc")
            ->paginate($pageSize, ["member.id", "member.username", "member.avatar", "member_recommend.create_time", "member_recommend.total_price"], null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);

        foreach ($res['items'] as $k => $v) {
            $res['items'][$k]['id_code'] = md6($v['id']);
        }

        return $res;
    }

    // 获取推广总数
    public function getTotal(int $ruid)
    {
        if (!$ruid) return 0;
        // status  学历认证
        // idcard_status  //实名认证
        return MemberRecommend::where("ruid", "=", $ruid)
            ->join("member", "member.id", "=", "member_recommend.uid")
//                    ->where("member.status", "=", 1)
            ->count();
    }

    /**
     * 统计异常推荐数据
     * @param null $uid
     */
    public function statExceptionRecommend($uid = null)
    {
        if (empty($uid) || in_array($uid, [545714, 48])) return true;

        //查看总邀请数
        $total = MemberRecommend::where(['ruid' => $uid])->count("id");
        if ($total < 10) return true;

        $total_ids_5 = MemberRecommend::where(['ruid' => $uid])
            ->whereBetween("create_time", [date("Y-m-d H:i", strtotime("-5 minute")), date("Y-m-d H:i")])
            ->pluck("id")->toArray();
        $total_subscribe_5 = Member::whereIn("id", $total_ids_5)->where("is_subscribe", 1)->count("id");

        $total_ids_30 = MemberRecommend::where(['ruid' => $uid])
            ->whereBetween("create_time", [date("Y-m-d H:i", strtotime("-30 minute")), date("Y-m-d H:i")])
            ->pluck("id")
            ->toArray();
        $total_subscribe_30 = Member::whereIn("id", $total_ids_30)->where("is_subscribe", 1)->count("id");

        $total_ids_day_ids = MemberRecommend::where(['ruid' => $uid])
            ->whereBetween("create_time", [date("Y-m-d H:i", strtotime("-1 day")), date("Y-m-d H:i")])
            ->pluck("id")
            ->toArray();
        $total_subscribe_day = Member::whereIn("id", $total_ids_day_ids)->where("is_subscribe", 1)->count("id");


        $e_data['total_5'] = count($total_ids_5);
        $e_data['total_subscribe_5'] = $total_subscribe_5;
        $e_data['total_30'] = count($total_ids_30);
        $e_data['total_subscribe_30'] = $total_subscribe_30;

        $e_data['total_day'] = count($total_ids_day_ids);
        $e_data['total_subscribe_day'] = $total_subscribe_day;

        if ($e_data['total_5'] < 3 || $e_data['total_30'] < 5 || $e_data['total_day'] < 10) return true;

        $precent_5 = count($total_ids_5) > 0 ? ($total_subscribe_5 / count($total_ids_5)) * 100 : 0;
        $precent_30 = count($total_ids_30) > 0 ? ($total_subscribe_30 / count($total_ids_30)) * 100 : 0;
        $precent_day = count($total_ids_day_ids) > 0 ? ($total_subscribe_day / count($total_ids_day_ids)) * 100 : 0;
//        && $precent_day < 20
        if ($precent_5 < 20 && $precent_30 < 20 ) return true;
        if ($precent_30 > 20 && $precent_5 > 20) {
            $level = 3;
        } else {
            $level = 2;
        }

        $except = MemberRecommendException::where(['uid' => $uid])->first();
        //当前等级低于之前的  不改变
        $e_data['precent_1'] = $precent_5;
        $e_data['precent_2'] = $precent_30;
        $e_data['level'] = 1;

        //5分钟内只记一次 ToDo 消息模板通知
        if ($except && strtotime($except['create_time']) > strtotime("-5 minute")) {
            return true;
        }

        $e_data['uid'] = $uid;
        MemberRecommendException::create($e_data);

        return true;
    }

}
